package com.zhangfb95.crosschain.plugin.client.demo.service.impl;

import com.zhangfb95.crosschain.protocol.routerpluginapi.PluginService;
import com.zhangfb95.crosschain.protocol.routerpluginapi.request.QueryPluginReq;
import com.zhangfb95.crosschain.protocol.routerpluginapi.request.SendPluginReq;
import com.zhangfb95.crosschain.protocol.routerpluginapi.response.GetBlockByNumberPluginRes;
import com.zhangfb95.crosschain.protocol.routerpluginapi.response.GetTxByIdPluginRes;
import com.zhangfb95.crosschain.protocol.routerpluginapi.response.QueryPluginRes;
import com.zhangfb95.crosschain.protocol.routerpluginapi.response.SendPluginRes;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

/**
 * @author zhangfubing
 * @since 2023/12/13
 */
public class DemoPluginServiceImpl implements PluginService {

    private final boolean enable;

    public DemoPluginServiceImpl(boolean enable) {
        this.enable = enable;
    }

    @Override
    public boolean isEnable() {
        return this.enable;
    }

    @Override
    public String getChainName() {
        return "payment.demo";
    }

    @Override
    public List<String> listChainPath() {
        return Arrays.asList("payment.demo.chain1", "payment.demo.chain2");
    }

    @Override
    public long getHeight(String chainPath) {
        return 9527L;
    }

    @Override
    public GetBlockByNumberPluginRes getBlockByNumber(String chainPath, Long blockNumber) {
        return new GetBlockByNumberPluginRes()
                .setBlockNumber(blockNumber)
                .setDataHash(blockNumber + "-blockHash")
                .setBlockHash(blockNumber + "-blockHash")
                .setPreviousHash((blockNumber - 1) + "-blockHash")
                .setBlockBytes("blockData".getBytes(StandardCharsets.UTF_8))
                .setTxIds(Arrays.asList(blockNumber + "-txId1", blockNumber + "-txId2"));
    }

    @Override
    public GetTxByIdPluginRes getTxById(String chainPath, String txId) {
        return new GetTxByIdPluginRes()
                .setTxId(txId)
                .setTransactionBytes(("txId:" + txId).getBytes(StandardCharsets.UTF_8));
    }

    @Override
    public QueryPluginRes query(QueryPluginReq req) {
        return new QueryPluginRes().setResult(req.toString());
    }

    @Override
    public SendPluginRes send(SendPluginReq req) {
        return new SendPluginRes()
                .setTransactionID(String.valueOf(System.currentTimeMillis()))
                .setResult(req.toString());
    }
}
